package com.itlzh.mall.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.itlzh.mall.common.enums.ResponseEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String message;
    private T data;

    public Result(Integer code, String message){
        this.code = code;
        this.message = message;
    }

    public Result(Integer code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(ResponseEnum responseEnum){
        this.code=responseEnum.getCode();
        this.message=responseEnum.getMessage();
    }

    public Result(ResponseEnum responseEnum, T data){
        this.code=responseEnum.getCode();
        this.message=responseEnum.getMessage();
        this.data=data;
    }

    //成功无数据返回
    public static <T> Result<T> success(){
        return new Result<>(ResponseEnum.SUCCESS);
    }
    //成功有数据返回
    public static <T> Result<T> success(T data){
        return new Result<>(ResponseEnum.SUCCESS,data);
    }

    /**
     * 失败，返回指定状态码和信息
     * @param responseEnum
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(ResponseEnum responseEnum){
        return new Result<>(responseEnum);
    }

    /**
     * 失败，返回默认错误信息
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(){
        return new Result<T>(ResponseEnum.FAIL);
    }

    /**
     * 失败，返回自定义错误信息
     * @param code
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(int  code,String message){
        return new Result<>(code,message);
    }

    /**
     * 失败，返回自定义错误信息,且有数据
     * @param code
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(int code,String message,T data){
        return new Result<>(code,message);
    }

    /**
     *  失败，返回指定状态码和信息,且有数据
     * @param responseEnum
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(ResponseEnum responseEnum,T data){
        return new Result<>(responseEnum,data);
    }
}
